package com.nuanshui.heatedloan.web.appsystem;

import com.alibaba.dubbo.config.annotation.Reference;
import com.google.common.collect.Maps;
import com.nuanshui.heatedloan.baseservice.facade.WeexVersionService;
import com.nuanshui.heatedloan.baseservice.form.WeexVersionForm;
import com.nuanshui.heatedloan.baseservice.vo.WeexVersionVO;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.pageable.Pager;
import com.nuanshui.heatedloan.web.ApiController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cms/manage/weexversion")
public class WeexVersionController extends ApiController{

	@Reference(url = GlobalParam.BASESERVICE_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.BASESERVICE_DUBBO_SERVER_VERSION)
	private WeexVersionService weexVersionService;
	
	@RequestMapping("/list")
	public String list(Pager<WeexVersionVO> pager){

		pager = weexVersionService.list(pager);

		List<WeexVersionVO> data = pager.getData();
		
		StringBuilder sb = new StringBuilder();
		for(WeexVersionVO vo : data){
			List<String> strs = vo.getAndroidNativeVersionCode();
			for(String str : strs){
				sb.append(str +",");
			}
			String st = sb.toString();
			if(st.length() >2){
				st = st.substring(0,  st.length()-1);
				vo.setAndroidNativeVersionCodeStr(st);
			}
			
			sb.setLength(0);
			
		}
		
		for(WeexVersionVO vo : data){
			List<String> strs = vo.getIosNativeVersionCode();
			for(String str : strs){
				sb.append(str +",");
			}
			String st = sb.toString();
			if(st.length() >2){
				st = st.substring(0,  st.length()-1);
				vo.setIosNativeVersionCodeStr(st);
			}
			sb.setLength(0);
		}

		return success(pager.asHashMap());
	}
	
	@RequestMapping("/delete")
	public String delete(String id){
		weexVersionService.delete(id);
		return success();
	}
	
	@RequestMapping("/changeStatus")
	public String changeStatus(String id, Integer status){
		weexVersionService.changeStatus(id, status);
		return success();
	}
	
	@RequestMapping("/detail")
	public String detail(String id){
		weexVersionService.loadVO(id);
		return success();
	}
	
	@RequestMapping("/create")
	public String create(WeexVersionForm form){
		weexVersionService.save(form);
		return success();
	}
	
}
